package com.example.spring.exercise.controller;

import com.example.spring.exercise.service.RougeService;
import com.example.spring.exercise.service.dto.rouge.RougeInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author : zhayh
 * @date : 2021-4-23 19:49
 * @description : 第4单元练习
 */

@RestController
@RequestMapping("/api/rouge/jpamybatis")
@Api(tags = "第4单元的API")
public class Unit4Controller {
    @Resource
    private RougeService rougeService;

    @PostMapping("/create-rouge")
    @ApiOperation(value = "添加一支口红")
    public ResponseEntity createRouge(@RequestBody RougeInfo rougeInfo) {
        rougeInfo = rougeService.insert(rougeInfo);
        if (rougeInfo != null) {
            return ResponseEntity.ok(rougeInfo);
        }
        return ResponseEntity.badRequest().body("添加失败");
    }
    @GetMapping("/country")
    @ApiOperation(value = "根据品牌所属国家选择价格区间的分页数据")
    public ResponseEntity getRougeByCountry(String countryName, Integer page, Integer size,
                                            Float minPrice, Float maxPrice) {
        Page<RougeInfo> rougeInfos = rougeService.getRougeByCountry(countryName, minPrice, maxPrice,
                page, size);
        return ResponseEntity.ok(rougeInfos);
    }

    @PutMapping("/update-name")
    @ApiOperation(value = "更新口红的名称")
    public ResponseEntity updateRougeName(Long id, String newName) {
        if(id == null || id == 0) {
            return ResponseEntity.badRequest().body("商品编码错误");
        }
        RougeInfo rougeInfo = rougeService.update(id, newName);
        if (rougeInfo != null) {
            return ResponseEntity.ok(rougeInfo);
        }
        return ResponseEntity.badRequest().body("更新失败");
    }
}
